package cn.swust.designpatternlearningplatform.service.impl;

import cn.swust.designpatternlearningplatform.common.Result.R;
import cn.swust.designpatternlearningplatform.dao.AboutDao;
import cn.swust.designpatternlearningplatform.domain.dto.AboutDto;
import cn.swust.designpatternlearningplatform.domain.entity.About;
import cn.swust.designpatternlearningplatform.service.AboutService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AboutServiceImpl implements AboutService {
    @Resource()
    private AboutDao aboutDao;
    @Override
    public R add(About about) {
        try {
            about.setTime(LocalDateTime.now());
            if (about.getNav()!=null&&about.getContent()!=null&&about.getTitle()!=null){
                aboutDao.add(about);
                return R.success("添加成功");
            }
            else {
                return R.error("添加失败，必要字段不为空");
            }

        }catch (Exception e){
            return R.error("添加失败");
        }
    }

    @Override
    public R update(About about) {
        try {
            aboutDao.update(about);
            return R.success("修改成功");
        }catch (Exception e)
        {
            return R.error("修改失败");
        }
    }

    @Override
    public R delete(List<Integer> list) {
        try {
            for (Integer integer : list) {
                aboutDao.delete(integer);
            }
            return R.success("删除成功");
        }catch (Exception e)
        {
            return R.error("删除失败");
        }

    }

    @Override
    public R selectAll() {
        List<About> list=aboutDao.selectAll();
        if (!list.isEmpty()){
            List<About> aboutList=new ArrayList<>();
            List<About> collect1 = list.stream().filter(About -> About.getIsFront().equals(1)).collect(Collectors.toList());
            aboutList.addAll(collect1);
            List<About> collect2 = list.stream().filter(About -> About.getIsFront().equals(0)).collect(Collectors.toList());
            if (!collect2.isEmpty()){
                List<About> collect3 = collect2.stream().sorted(Comparator.comparing(About::getTime)).collect(Collectors.toList());
                aboutList.addAll(collect3);
            }
//            Map<String, List<About>> collect = list.stream().collect(Collectors.groupingBy(About::getNav));
//            for (Map.Entry<String,List<About>> entry: collect.entrySet()){
//                List<About> aboutList=new ArrayList<>();
//                List<About> value = entry.getValue();
//                if (!value.isEmpty()){
//                    List<About> collect1 = value.stream().filter(About -> About.getIsFront().equals(1)).collect(Collectors.toList());
//                    aboutList.addAll(collect1);
//                    List<About> collect2 = value.stream().filter(About -> About.getIsFront().equals(0)).collect(Collectors.toList());
//                    if (!collect2.isEmpty()){
//                        List<About> collect3 = collect2.stream().sorted(Comparator.comparing(About::getTime)).collect(Collectors.toList());
//                        aboutList.addAll(collect3);
//                    }
//
//                }
//                entry.setValue(aboutList);
//            }
            AboutDto aboutDto=new AboutDto();
            aboutDto.setList(aboutList);
            aboutDto.setTotal(aboutList.size());
            return R.success(aboutDto);
        }
        return R.success(null);
    }
}
